/*
 * DailyQuotationProvider.java
 * business
 *
 * Created by ChenTao on 2017/6/5.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.ui.goodsdetail.GoodsDetailActivity;
import com.yidiandao.business.ui.widget.dialog.QuickBuyDialog;
import com.yidiandao.business.ui.widget.view.FlagView;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.PriceUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 每日行情-Provider
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017-06-05 10:08
 */
public class DailyQuotationProvider extends ItemViewProvider<Goods, DailyQuotationProvider.ViewHolder> {

    private Activity mActivity;

    public DailyQuotationProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        return new ViewHolder(inflater.inflate(R.layout.item_daily_quotation, parent, false));
    }

    private SpannableStringBuilder getSingePrice(String singlePrice) {
        SpannableStringBuilder builder = new SpannableStringBuilder();

        SpannableString pre = TextRenderUtils.getAbsoluteSizeText(UIUtils.getString(R.string.single_hint_2), UIUtils.dp2px(13));
        SpannableString suf = TextRenderUtils.getBeautyPrice(singlePrice, null, UIUtils.dp2px(13), UIUtils.dp2px(13));

        builder.append(pre);
        builder.append(suf);

        return builder;
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final Goods goods) {
        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivGoodsImage, goods.getGoodsThumb());
        PriceUtils.setDailyPrice(holder.tvQuotation, goods.getPlusPrice());

        holder.tvBottlePrice.setText(getSingePrice(goods.getBottlePrice() + ""));
        holder.tvGoodsName.setText(goods.getGoodsName());
        holder.tvGoodsPrice.setText(TextRenderUtils.getBeautyPrice(goods.getGoodsPrice() + "", goods.getGoodsUnit(), UIUtils.dp2px(18), UIUtils.dp2px(14)));
        if (goods.getIsSpecial() == 1) {
            holder.flagView.setVisibility(View.VISIBLE);
            holder.flagView.setFlagText("特价", FlagView.TYPE_DISCOUNT);
        } else {
            holder.flagView.setVisibility(View.GONE);
        }

        holder.ivShoppingCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getGoodsCartInfo(goods);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GoodsDetailActivity.actionStart(mActivity, goods.getGoodsId());
            }
        });

        int stockNumber = goods.getStockNumber();
        holder.tvSellOut.setVisibility(stockNumber > 0 ? View.GONE : View.VISIBLE);
        holder.ivShoppingCart.setVisibility(stockNumber > 0 ? View.VISIBLE : View.GONE);
    }

    private void getGoodsCartInfo(final Goods goods) {

        // 获取商品的购物车信息
        float goodsPrice = goods.getGoodsPrice();
        int goodsNumber = 1;
        int stockNumber = goods.getStockNumber();
        String goodsId = goods.getGoodsId();
        int minNumber = goods.getMinNumber();
        int limitNumber = goods.getLimitNumber();
        // 最少起订量大于库存,无法购买
        if (minNumber > stockNumber) {
            UIUtils.showToast(UIUtils.getString(R.string.can_not_buy_when_out_of_stock));
            return;
        }

        Goods newGoods = new Goods();
        newGoods.setGoodsId(goodsId);
        newGoods.setStockNumber(stockNumber);
        newGoods.setGoodsName(goods.getGoodsName());
        newGoods.setGoodsPrice(goodsPrice);
        newGoods.setGoodsNumber(goodsNumber);
        newGoods.setMinNumber(minNumber);
        newGoods.setLimitNumber(limitNumber);
        // 没有优惠活动，则显示立即立即购买，否则不显示立即购买
        newGoods.setFavourableNotice(goods.getFavourableNotice());

        showAddToCartDialog(newGoods);
    }

    private void showAddToCartDialog(Goods goods) {

        QuickBuyDialog dialog = new QuickBuyDialog(mActivity);
        dialog.show();

        dialog.setGoods(goods);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivGoodsImage;
        private TextView tvGoodsName;
        private TextView tvQuotation;
        private TextView tvGoodsPrice;
        private ImageView ivShoppingCart;
        private TextView tvBottlePrice;
        private FlagView flagView;
        private TextView tvSellOut;

        public ViewHolder(View itemView) {
            super(itemView);

            flagView = (FlagView) itemView.findViewById(R.id.flag_view);
            ivGoodsImage = (ImageView) itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tvQuotation = (TextView) itemView.findViewById(R.id.tv_quotation);
            tvGoodsPrice = (TextView) itemView.findViewById(R.id.tv_goods_price);
            ivShoppingCart = (ImageView) itemView.findViewById(R.id.iv_shopping_cart);
            tvBottlePrice = (TextView) itemView.findViewById(R.id.tv_bottle_price);
            tvSellOut = (TextView) itemView.findViewById(R.id.tv_sell_out);
        }
    }
}
